<template>
	<view>
		购物车
		<!-- v-model不支持多选框 v-model仅仅支持输入框 -->
		<!-- <checkbox v-model="isChecked" /> -->
		
		<view class="list" v-for="item in list" :key="item.id">
			 <evan-checkbox v-model="item.isChecked"></evan-checkbox> 
				 <view>
					 商品名称{{ item.productName }} 商品价格{{ item.price }} 商品数量{{ item.quantity }}
				 </view>
				  
		</view>
		
		<view>
			全选 <evan-checkbox v-model="isAllChecked"></evan-checkbox> 
		</view>
		<button type="default" @tap="buy">
			结算
		</button>
	</view>
</template>

<script>
	import {
		findAllCartItem
	} from "@/api/user/car/index.js"
	import {
		addPreOrder
	} from "@/api/kind/list/info/index.js"
	import mix from "@/mixins/index.js"
	export default {
		data() {
			return {
				list:[],
				// isAllChecked:false
			};
		},
		computed:{
			// isAllChecked(){
			// 	return this.list.every(ele =>ele.isChecked)
			// }
			isAllChecked:{
				get(){
					return this.list.every(ele =>ele.isChecked)
				},
				set(val){
					// if()
					this.list.forEach(ele =>ele.isChecked = val)
				}
			}
		},
		mixins:[mix],
		methods:{
			buy(){
				var obj = {
				  "orderItemList": [
					// {
					//   "cartId": "string",
					//   "productId": "string",
					//   "productQuantity": 0,
					//   "productSkuId": "string"
					// }
				  ]
				}
				// addPreOrder()
				var arr = this.list.filter(ele =>ele.isChecked == true);
				arr.forEach(ele =>{
					var obj1 = {
					  "cartId": ele.id,
					  "productId": ele.productId,
					  "productQuantity": ele.quantity,
					  "productSkuId": ele.productSkuId
					}
					obj.orderItemList.push(obj1)
				})
				addPreOrder(obj)
			}
		},
		onLoad() {
			// 
			if(this.checkLogins()){
				findAllCartItem()
				.then(res =>{
					console.log(res);
					res.data.items.forEach(ele =>ele.isChecked = false);
					this.list = res.data.items;
				})
			}
			// findAllCartItem()
			
		}
	}
</script>

<style lang="scss" scoped>
.list{
	padding: 10px;
	display: flex;
	align-items: center;
	flex-wrap: wrap;
}
</style>
